<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="dashboard.User"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Calendar" %>
<%@page import="java.util.List"%>
<%@page import="dashboard.Course" %>
<%@page import="dashboard.CourseRegistry" %>
<%@page import="java.util.ArrayList"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>statistics</title>

</head>

<script type="text/javascript">
)
function validate(evt) {
	 if(evt.keyCode != 46 && evt.keyCode != 8 && evt.keyCode != 9 && evt.keyCode != 27 && evt.keyCode != 13 )
	  {
	  var theEvent = evt || window.event;
	  var key = theEvent.keyCode || theEvent.which;
	  key = String.fromCharCode( key );
	  var regex = /[0-9]|\./;
	  if( !regex.test(key) ) {
	    theEvent.returnValue = false;
	    if(theEvent.preventDefault) theEvent.preventDefault();
	  }
	  }
	}
	
</script>

<body>
	<div id="main">
		<%@include file="header.jsp"%>
		<%
	List<String> errors = (List<String>) session.getAttribute("errors");
		if (errors != null) {
					%>

		<div id=error>
			<p class="warning">
				An error occurred:
				<%
					for(String error: errors){
						%><%=error%><br>
				<%
					}
					%>
			</p>
		</div>
		<%
		}
		%>
		<!-- BEGIN KEUZEMENU -->

		<% 
User currentUser2= (User) session.getAttribute("user");
if(currentUser2==null){
response.sendRedirect("login.jsp");
}
else{
		String text = currentUser2.getFirstName() + " " +currentUser2.getName();
		if(request.getParameter("ref")!=null){
			session.removeAttribute("rNumber");
		}
		if(session.getAttribute("rNumber")!=null){
			text= (String) session.getAttribute("rNumber");
		}
		
		
if(request.getParameter("s")==null){
	
 %>
		<fieldset>
			<legend> Statistics menu : <%=text %></legend>
			<form name="getR" method="post" action="Statistics">
				<% if (currentUser2.getRole()==User.Role.TUTOR){%>
				Fill in r-number. <br>
				r<input type="text" name="rNumber" value="" onkeypress='validate(event)' class="mobileStyle">
				<br>
				<input  type="submit" value="Get stats">
				</form>
				<%
}

%>				
				<form name="getStatistics" method="post" action="Statistics">
				<div class="button">
					<input style="width: 100%; text-align: center" type="submit" name="month" value="Monthly view">
				
				
					<input style="width: 100%; text-align: center" type="submit" name="live" value="Live Users">
				
			
					<input style="width: 100%; text-align: center" type="submit" name="course" value="Stats per Course">
				
				
					<input style="width: 100%; text-align: center" type="submit" name="milestones" value="Milestones">
				
			
					<input style="width: 100%; text-align: center" type="submit" name="global" value="Global">
				</div>
			</form>
			
			<br>
			
			<form method="post" action="dashboard.jsp" name="backMenu">
				<div class="button">
					<input type="submit" value="Back" name="back">
				</div>
			</form>
		</fieldset>
		<!-- EIND KEUZEMENU -->
		<% 
}


// BEGIN MONTHLY STATS

else if(request.getParameter("s").equals("month")){

if(request.getParameter("m")==null){%>
	<fieldset>
			<legend> Pick a month</legend>
				<div class="button">
			<form name="getStatistics" method="post" action="Statistics">
			<input type="hidden" name="month" value="month">
					
<%
String[] monthName = { "October", "November",
        "December","January", "February",
        "March", "April", "May", "June", "July",
        "August", "September"};

for(int i=0;i<12;i++){
	%><input style="width: 100%; text-align: center" type="submit" name="monthName" value="<%=monthName[i]%>"><%
}


%>
			
			</form></div>

			<br>
			
			<form method="post" action="statistics.jsp" name="backMenu">
				<div class="button">
					<input type="submit" value="Back" name="back">
				</div>
			</form>
				</fieldset>
		<%
}

else {
	
	String month = (String) request.getParameter("m");
	ArrayList<String> cats = (ArrayList<String>) session.getAttribute("monthNames");
	LinkedHashMap<String, Double> appreciation = new LinkedHashMap<String, Double>();
	LinkedHashMap<String, Double> averageAppreciation = new LinkedHashMap<String, Double>();
	LinkedHashMap<String, Double[]> userMonthMap = (LinkedHashMap<String,Double[]>) session.getAttribute("userMonthMap");
	LinkedHashMap<String, Double[]> averageMonthMap =(LinkedHashMap<String, Double[]>) session.getAttribute("averageMonthMap");
	LinkedHashMap<String,Double> otherStuff = (LinkedHashMap<String,Double>) session.getAttribute("monthStats");
	LinkedHashMap<String, Double> userTypeMonth = (LinkedHashMap<String,Double>) session.getAttribute("userTypeMonth");
	LinkedHashMap<String, Double> averageTypeMonth = (LinkedHashMap<String, Double>) session.getAttribute("averageTypeMonth");
	double container = 0.0; 
	session.setAttribute("container",container);
	
	
%>
	<fieldset>
	<legend> <%=month%> : <%=text %></legend>
				<div class="button">
			<font size=4><a href="javascript:history.go(-1)">Back</a></font>
				</div>
<div class="button">
<% if(session.getAttribute("rNumber")!=null) {%>
<input type="submit" value="Back to your favorites" onclick="location.href='Favorites.jsp?action=view';">
<%} %>
	
</div>
<%
if(userMonthMap!=null && averageMonthMap!=null){
	String title = "Total activity in the month "+ month;
	LinkedHashMap<String, Double[]> ownMap = userMonthMap;
	LinkedHashMap<String,Double[]> averageMap = averageMonthMap;
	String yAxis="Total hours spent";
	session.setAttribute("title",title);
	session.setAttribute("ownMap",ownMap);
	session.setAttribute("averageMap",averageMap);
	session.setAttribute("yAxis",yAxis);
	session.setAttribute("cats", cats);
	%>
	
	<%@ include file="../charts/stackChart.jsp"%>
	<%
	container++;
	session.setAttribute("container",container);
}

if(otherStuff!=null){
	%>
	<table>
	<tbody>
	<%
	Iterator it = otherStuff.entrySet().iterator();
	while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
	String name = (String) pairs.getKey();
	double value  = (Double) pairs.getValue();
	value=100*value;
	value = Math.round(value);
	value=value/100;
	%>
	<tr><td style="padding-left: 50px;"><b><%=name%></b>: </td><td style="text-align: center; width: 150px;"> <%=value%></td></tr> <%
	}
	%>
	</tbody></table>
	<%	
}
	if(averageTypeMonth!=null){
		LinkedHashMap<String, Double> result = averageTypeMonth;
		
		String title="Average Time spent in category by all users";
		String subtitle = "Hours of activity";
		session.setAttribute("result",result);
		session.setAttribute("title",title);
		session.setAttribute("subtitle",subtitle);
	%>

	<%@ include file="../charts/pieChart.jsp"%>
	<%
	container++;
	session.setAttribute("container",container);
	}
	
	if(userTypeMonth!=null){
		LinkedHashMap<String, Double> result = userTypeMonth;
		String title="Time spent this month";
		String subtitle = "Hours of activity";
		session.setAttribute("title",title);
		session.setAttribute("subtitle",subtitle);
		session.setAttribute("result",result);
	
	%>

	<%@ include file="../charts/pieChart.jsp"%>
	<%
	container++;
	session.setAttribute("container",container);
	}

	

%>
	</fieldset>
<%

}

}
// END MONTHLY STATS





//BEGIN LIVE STATS
else if(request.getParameter("s").equals("live") && session.getAttribute("result")!=null){
	LinkedHashMap<String,Double > result = (LinkedHashMap<String, Double>) session.getAttribute("result");
	
	 if(request.getParameter("type")!=null){
			%><fieldset><legend>Live Stats of <%=request.getParameter("type")%></legend>
			<%	 
		String title ="Number of people currently tracking an activity of the type " + request.getParameter("type");
		String subtitle = "Ordered by course";
			
			session.setAttribute("title",title);
			session.setAttribute("subtitle",subtitle);
			session.setAttribute("result",result);
			session.setAttribute("container",0.0);
		  %>

			<%@ include file="../charts/pieChart.jsp"%>
			
			
			<form method="post" action="Statistics">
			<input style="width: 100%; text-align: center" type="hidden" name="live" value="Live Users">
			<input type="submit" value="Back">
			
			</form>
			</fieldset>
		 <%
		 
	 	}
	 else{		 
		 
		 %><fieldset><legend>Who's doing what</legend>	
	<div class="button">
			<font size=4><a href="statistics.jsp">Back</a></font>
				</div>
			
		 <%
			String title="Number of People currently tracking an activity";
	
			String subtitle = "According to type";
		

			session.setAttribute("container",0.0);
			session.setAttribute("title",title);
			session.setAttribute("subtitle",subtitle);
			session.setAttribute("result",result);
		    %>

			<%@ include file="../charts/pieChart.jsp"%>
			Get more detailed information about :<br>
			<form method="post" action="Statistics">
	   <select name="type">
			<%
		 
	 Iterator it3 = result.entrySet().iterator();
	    while (it3.hasNext()) {
	    Map.Entry pairs = (Map.Entry)it3.next();
	    String typeName = (String) pairs.getKey();
	   %>
	<option><%=typeName%></option>
<%	    } %>
	</select>
	
		<input type="submit" name="giveType" value="Get results">
		<input type="hidden" name="live">
			<div class="button"></div>
			</form>
			<%
		 %></fieldset><%
		 
	 }

}
// END LIVE STATS




//BEGIN Course STATS
else if(request.getParameter("s").equals("course")){
	if(request.getParameter("course")==null){
		%><fieldset>
		<legend> Course Statistics: <%=text %></legend>
			<div class="button" >
					<% if(session.getAttribute("rNumber")!=null && !currentUser2.getRole().equals(User.Role.TUTOR)) {%>
<input type="submit" value="Back to your favorites" onclick="location.href='Favorites.jsp?action=view';">
<%} %>
				
			
		Enrolled Courses:<br> <br>

		<form name="getStats" method="post" action="Statistics">
		<%
		User reqUser = currentUser2;
		if(session.getAttribute("rNumber")!=null) {
			
			String userName = (String) session.getAttribute("rNumber");
			if(UserRegistry.getInstance().getUserByUserName(userName)!=null){
				reqUser = UserRegistry.getInstance().getUserByUserName(userName);
			}
			else if(UserRegistry.getInstance().getUserByNumber(userName)!=null){
				reqUser = UserRegistry.getInstance().getUserByNumber(userName);
			}
		}
		
		
		List<Long> courses = reqUser.getCourses();
		if(!courses.isEmpty()){
			%>
	
			<select name="courseName" style="width: 100%; text-align: center; height: 25px; font-size: 16px;">
			<%
			for(long courseId : courses){
				String courseName = CourseRegistry.getInstance().getCourseName(courseId);
				%>
				<option><%=courseName%></option>
				<%
			}
			%></select><br>
			<input type="hidden" value="course" name="course">
			<br>
			<input style="width: 100%; text-align: center" type="submit" value="Get Stats" name="getStats">
		</form>
				</div>
		
		<br>
		
		<div class="button">
			<button onclick="location.href='statistics.jsp'">Back</button>
				</div>
	<%
	}
	%></fieldset><%
	}
	else{
		String courseName = request.getParameter("course");
		double container = 0;
		session.setAttribute("container", container);
		%>
		<fieldset>
	<legend>Statistics of <%=courseName %> : <%=text %></legend>
	<div class="button">
	<font size=4><a href="statistics.jsp?s=course">Back</a></font></div>
<% if(session.getAttribute("rNumber")!=null  && !currentUser2.getRole().equals(User.Role.TUTOR)) {%>
<input type="submit" value="Back to your favorites" onclick="location.href='Favorites.jsp?action=view';">
<%} %>

		<%
	
	LinkedHashMap<String, Double> userDuration = (LinkedHashMap<String,Double>) session.getAttribute("userDuration");
	LinkedHashMap<String, Double> totalDuration = (LinkedHashMap<String,Double>) session.getAttribute("totalDuration");
	LinkedHashMap<String, Double> userWeekDuration = (LinkedHashMap<String,Double>) session.getAttribute("userWeekDuration");
	LinkedHashMap<String, Double> allWeekDuration = (LinkedHashMap<String,Double>) session.getAttribute("allWeekDuration");
	LinkedHashMap<String, Double> userMonthDuration = (LinkedHashMap<String,Double>) session.getAttribute("userMonthDuration");
	LinkedHashMap<String, Double> allMonthDuration = (LinkedHashMap<String,Double>) session.getAttribute("allMonthDuration");
	LinkedHashMap<String, Double> userYearDuration = (LinkedHashMap<String,Double>) session.getAttribute("userYearDuration");
	LinkedHashMap<String, Double> allYearDuration = (LinkedHashMap<String,Double>) session.getAttribute("allYearDuration");
	ArrayList<Double> appreciation = (ArrayList<Double>) session.getAttribute("courseAppreciation");
	
    double averageAp = appreciation.get(0) * 100;
    averageAp = Math.round(averageAp);
    averageAp = averageAp / 100;
	
    double yourAp = appreciation.get(1) * 100;
    yourAp = Math.round(yourAp);
    yourAp = yourAp / 100;
	
%><b>Your average Appreciation : </b><%=averageAp%><br>
<b>Average Student Appreciation :</b> <%=yourAp %>
<%
	// Totale Activiteit
	if(userDuration!=null && totalDuration!=null){
		String title="Total Activity";
		String subtitle="In the current schoolyear";
		String yAxis="Amount of hours";
	    LinkedHashMap<String, Double> ownMap = userDuration;
	    LinkedHashMap<String,Double> averageMap = totalDuration;
		String unit = "hours";
	
		session.setAttribute("title",title);
		session.setAttribute("subtitle",subtitle);
		session.setAttribute("ownMap",ownMap);
		session.setAttribute("averageMap",averageMap);
		session.setAttribute("yAxis",yAxis);
		session.setAttribute("unit",unit);
		%>
			<%@ include file="../charts/colChart.jsp"%>
		<%
		container++;
		session.setAttribute("container",container);
  	}
  	
	// Wekelijkse statistieken
	if(userWeekDuration!=null && allWeekDuration!=null){
		String title="Activity this week";
		String subtitle="compared to average student";
		String yAxis="Amount of hours";
	    LinkedHashMap<String, Double> ownMap = userWeekDuration;
	    LinkedHashMap<String,Double> averageMap = allWeekDuration;
	    session.setAttribute("title",title);
	    session.setAttribute("subtitle",subtitle);
		session.setAttribute("ownMap",ownMap);
		session.setAttribute("averageMap",averageMap);
		session.setAttribute("yAxis",yAxis);

	    
		%>
		<%@ include file="../charts/lineChart.jsp"%>
	
	<%
	container++;
	session.setAttribute("container",container);
	}

	
	// Maandelijkse statistieken
	if(userMonthDuration!=null && allMonthDuration!=null){
		String[] monthName = {"January", "February",
	            "March", "April", "May", "June", "July",
	            "August", "September", "October", "November",
	            "December"};
	 
	       Calendar cal = Calendar.getInstance();
	    String month = monthName[cal.get(Calendar.MONTH)];
		String title="Activity in " + month;
		String subtitle="compared to the average student";
		String yAxis="Amount of hours";
	    LinkedHashMap<String, Double> ownMap = userMonthDuration;
	    LinkedHashMap<String,Double> averageMap = allMonthDuration;
	    session.setAttribute("title",title);
		session.setAttribute("ownMap",ownMap);
		session.setAttribute("averageMap",averageMap);
		session.setAttribute("yAxis",yAxis);

		%>
		<%@ include file="../charts/lineChart.jsp"%>
	<%
	container++;
	session.setAttribute("container",container);
	}

	if(userYearDuration!=null && allYearDuration!=null){
		String title="Activity this year";
		String subtitle="compared to average student";
		String yAxis="Amount of hours";
	    LinkedHashMap<String, Double> ownMap = userYearDuration;
	    LinkedHashMap<String,Double> averageMap = allYearDuration;
	    session.setAttribute("title",title);
		session.setAttribute("ownMap",ownMap);
		session.setAttribute("averageMap",averageMap);
		session.setAttribute("yAxis",yAxis);
	
		%>
		<%@ include file="../charts/lineChart.jsp"%>
	<%
	container++;
	session.setAttribute("container",container);
	}
	%>
<div class="button">
	<font size=4><a href="statistics.jsp">Back</a></font></div>
	</fieldset>
			<%
	
}
	
}
//END COURSE STATS





//BEGIN GLOBAL STATS
else if(request.getParameter("s").equals("global") && session.getAttribute("allActivities")!=null){
	%>
<fieldset>
				<legend> Global Stats : <%=text %></legend>

	
		<div class="button">
	<font size=4><a href="statistics.jsp">Back</a></font></div>
<% if(session.getAttribute("rNumber")!=null  && !currentUser2.getRole().equals(User.Role.TUTOR)) {%>
<input type="submit" value="Back to your favorites" onclick="location.href='Favorites.jsp?action=view';">
<%} %>

<% 
	double container=0; 
	LinkedHashMap<String, Double> allActivities = (LinkedHashMap<String,Double>) session.getAttribute("allActivities");
	LinkedHashMap<String, Double> userActivities = (LinkedHashMap<String,Double>) session.getAttribute("userActivities");
	LinkedHashMap<String, Double> allCourses = (LinkedHashMap<String, Double>) session.getAttribute("allCourses");
	LinkedHashMap<String, Double> allCourseAppreciation = (LinkedHashMap<String, Double>) session.getAttribute("coursesAppreciation");
	LinkedHashMap<String, Double> userCourseAppreciation = (LinkedHashMap<String, Double>) session.getAttribute("userCoursesAppreciation");
	session.setAttribute("container", container);
	%>			
<%
if(userActivities!=null){
	LinkedHashMap<String, Double> result=userActivities;
	String title="Your Activities";
	String subtitle = "Hours of activity";
	session.setAttribute("title",title);
	session.setAttribute("subtitle",subtitle);
	session.setAttribute("result",result);
	
	%>
	<%@ include file="../charts/pieChart.jsp"%><%
	container++;
	session.setAttribute("container",container);
}
if(allActivities!=null){
	LinkedHashMap<String, Double> result=allActivities;
	String title="Average Activities";
	String subtitle = "Hours of activity";
	session.setAttribute("title",title);
	session.setAttribute("subtitle",subtitle);
	session.setAttribute("result",result);
	%>
	<%@ include file="../charts/pieChart.jsp"%><%
	container++;
	session.setAttribute("container",container);
}
if(allCourses!=null){
	LinkedHashMap<String, Double> result=allCourses;
	String title="StudyPoints";
	String subtitle = "1 studypoint equals 30 hours of activity. ";
	
	session.setAttribute("title",title);
	session.setAttribute("subtitle",subtitle);
	session.setAttribute("result",result);
	%>
	<%@ include file="../charts/pieChart.jsp"%><%
	container++;
	session.setAttribute("container",container);
}

if(allCourseAppreciation!=null && userCourseAppreciation!=null){
	LinkedHashMap<String, Double> averageMap=allCourseAppreciation;
	LinkedHashMap<String, Double> ownMap = userCourseAppreciation;
	String title="Average Appreciation of Courses";
	String subtitle = "Between 0 and 10.";
	String yAxis = "appreciation";
	String unit = "/ 10";
	   session.setAttribute("title",title);
	   session.setAttribute("subtitle", subtitle);
			session.setAttribute("ownMap",ownMap);
			session.setAttribute("averageMap",averageMap);
			session.setAttribute("yAxis",yAxis);
		session.setAttribute("unit",unit);
	%>
	<%@ include file="../charts/colChart.jsp"%><%
	
}
%>
				<div class="button">
	<font size=4><a href="statistics.jsp">Back</a></font></div>
			</fieldset>
			<%
}

//END GLOBAL STATS




//BEGIN MILESTONES STATS
else if(request.getParameter("s").equals("milestones") && session.getAttribute("milestones")!=null){
	LinkedHashMap<String, Double> result = (LinkedHashMap<String, Double>) session.getAttribute("milestones");
	double container=0;
	String title="Statistieken Milestones";
	String subtitle="";
	session.setAttribute("title",title);
	session.setAttribute("subtitle",subtitle);
	session.setAttribute("result",result);
	session.setAttribute("container",container);
	
	%><fieldset>
		
				<legend> Milestones : <%=text %></legend>
				<% if(session.getAttribute("rNumber")!=null  && !currentUser2.getRole().equals(User.Role.TUTOR)) {%>
<input type="submit" value="Back to your favorites" onclick="location.href='Favorites.jsp?action=view';">
<%} 
%>
				<%@ include file="../charts/pieChart.jsp"%>
			<div class="button">
	<font size=4><a href="statistics.jsp">Back</a></font></div>
			</fieldset>
			<%
}
// END MILESTONE STATS

else{
	%>
			<div id=error>
				<p class="warning">
					This page is unavalaible.<br> <a href="statistics.jsp">
						Back</a>
				</p>
			</div>
			<%
}
}

session.removeAttribute("courseName");
session.removeAttribute("errors");
%>

	</div>
</body>
</html>
